//
//  AnimationUtil.swift
//  Runner
//
//  Created by 麻再挺 on 2019/3/26.
//  Copyright © 2019 The Chromium Authors. All rights reserved.
//

import Foundation

// 动画工具类
class AnimationUtil {
    // 弹窗动画
    // @param duration 显示时长， 默认值为1.5s
    // @return 动画
    static func getToastAnimation(duration: CFTimeInterval = 1.5) -> CAAnimation {
        // 大小变化动画
        let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        // 设置时间
        scaleAnimation.keyTimes = [0, 0.1, 0.9, 1]
        // 设置值
        scaleAnimation.values = [0.5, 1, 1, 0.5]
        // 设置显示时间
        scaleAnimation.duration = duration
        
        // 透明度变化动画
        let opacityAnimation = CAKeyframeAnimation(keyPath: "opacity")
        // 设置时间
        opacityAnimation.keyTimes = [0, 0.8, 1]
        // 设置值
        opacityAnimation.values = [0.5, 1, 0]
        // 设置显示时长
        opacityAnimation.duration = duration
        
        // 组动画
        let animation = CAAnimationGroup()
        // 设置动画组
        animation.animations = [scaleAnimation, opacityAnimation]
        // 动画过渡效果
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        // 设置时间
        animation.duration = duration
        // 设置重复次数
        animation.repeatCount = 0
        // 设置完成后移除
        animation.isRemovedOnCompletion = false
        return animation
    }
}
